package com.curluin.inputInterpretation;

import android.hardware.Sensor;
import android.hardware.SensorEvent;

import com.curluin.math.MyVector;

public class TiltEvent {
	

	private static final float[] thresholds={2,0,Float.POSITIVE_INFINITY};

	public static final float maxAccelerationPerMsec = 9.81f*1000;
	
//	/**
//	 * Event preceeding this.
//	 */
//	private TiltEvent previousEvent;
	/**
	 * Acceleration in the different dimensions.
	 */
	private MyVector acceleration;
	
//	private long timestamp;
	
	public TiltEvent(SensorEvent event){
//		this.previousEvent=previousEvent;
		if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
			acceleration=MyVector.makeVector3(applyThreshold(0, event), applyThreshold(1, event), applyThreshold(2, event));
		}
//		timestamp=event.timestamp;
	}
	
	private float applyThreshold(int dimension,SensorEvent event){
		float res=0;
		float sensorData=event.values[dimension];
		if(Math.abs(sensorData)>thresholds[dimension]){
			float factor=(sensorData>=0)?1:-1;
			if(dimension==1)factor=(sensorData>=0)?-1:1;
			res=factor*(thresholds[dimension]-Math.abs(sensorData));
		}
		return res;
	}
	
	/**
	 * Acceleration(m/s) in each Dimension, threshold must be passed 
	 * @return
	 */
	public MyVector getAcceleration(){
		return acceleration;
	}
//	
//	/**
//	 * Acceleration-speed of tilt in relation to previous event.
//	 * @return
//	 */
//	public MyVector getAccSpeed(){
//		//TODO for now only for testing. 
//		if(previousEvent==null) return MyVector.makeVector3(0, 0, 0);
//		return acceleration.subtract(previousEvent.acceleration);
//	}
//	
//	/**
//	 * Time between this and previous TiltEvent in ms
//	 * @return
//	 */
//	public float getTimeDiffToLast(){
//		float msPerNs=1000000.0;
//		return (timestamp-previousEvent.timestamp)/msPerNs;
//	}
}
